package com.tong.embeddedvr.timer;

import com.tong.embeddedvr.Mapper.HostAccountMapper;
import com.tong.embeddedvr.Mapper.HostMapper;
import com.tong.embeddedvr.dto.HostAccountDTO;
import com.tong.embeddedvr.dto.HostDTO;
import com.tong.embeddedvr.entity.HostAccountEntity;
import com.tong.embeddedvr.entity.HostEntity;
import com.tong.embeddedvr.service.impl.SSHServiceImpl;
import com.tong.embeddedvr.utils.CommonUtil;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.TimerTask;

/**
 * TODO 将延迟时间修改为product环境
 * @Description 继承了TimerTask类,可以完成一些精度要求不高的倒计时任务
 * @Author gcl
 * @Date 2021/3/7 18:12
 * @param: null
 * @Return
 */
public class ShellTimer extends TimerTask {

    private SSHServiceImpl sshService;

    private HostAccountMapper hostAccountMapper;
    private HostMapper hostMapper;

    private HostAccountDTO hostAccount;
    private HostDTO host;
    public static long DELAY_TIME = 1000*60*60*3;  //product,定时延迟触发的时间
//    public static long DELAY_TIME = 1000*30;  //development,定时延迟触发的时间

    private final Logger logger = LoggerFactory.getLogger(ShellTimer.class);

    public ShellTimer() {
    }

    public ShellTimer(SSHServiceImpl sshService,HostAccountMapper hostAccountMapper, HostMapper hostMapper, HostAccountDTO hostAccount, HostDTO host, Long delayTime) {
        this.sshService = sshService;
        this.hostAccountMapper = hostAccountMapper;
        this.hostMapper = hostMapper;
        this.hostAccount = hostAccount;
        this.host = host;
    }

    public SSHServiceImpl getSshService() {
        return sshService;
    }

    public void setSshService(SSHServiceImpl sshService) {
        this.sshService = sshService;
    }

    public HostAccountMapper getHostAccountMapper() {
        return hostAccountMapper;
    }

    public void setHostAccountMapper(HostAccountMapper hostAccountMapper) {
        this.hostAccountMapper = hostAccountMapper;
    }

    public HostMapper getHostMapper() {
        return hostMapper;
    }

    public void setHostMapper(HostMapper hostMapper) {
        this.hostMapper = hostMapper;
    }

    public HostAccountDTO getHostAccount() {
        return hostAccount;
    }

    public void setHostAccount(HostAccountDTO hostAccount) {
        this.hostAccount = hostAccount;
    }

    public HostDTO getHost() {
        return host;
    }

    public void setHost(HostDTO host) {
        this.host = host;
    }

    @Override
    public String toString() {
        return "ShellTimer{" +
                "sshService=" + sshService +
                ", hostAccountMapper=" + hostAccountMapper +
                ", hostMapper=" + hostMapper +
                ", hostAccount=" + hostAccount +
                ", host=" + host +
                ", DELAY_TIME=" + DELAY_TIME +
                '}';
    }

    /**
     * 这里使用@Transaction注解有错误
     */
    @Override
    public void run() {
        System.out.println("定时器触发");
        DefaultMapperFactory mapperFactory = CommonUtil.getMapperFactory();
        //先更新host_account_state参数
        int userId = hostAccount.getUserId();
        hostAccount.setHostAccountState(0);  //已超时
        HostAccountEntity hostAccountProp = mapperFactory.getMapperFacade().map(hostAccount, HostAccountEntity.class);
        hostAccountMapper.updateInformation(hostAccountProp);
        //尝试删除账号
        try {
            sshService.userdel(host,hostAccount);
            hostAccount.setHostAccountIsDelete(1);  //删除该账号
            hostAccountProp = mapperFactory.getMapperFacade().map(hostAccount,HostAccountEntity.class);
            hostAccountMapper.updateInformation(hostAccountProp);
            HostEntity hostProp = mapperFactory.getMapperFacade().map(host, HostEntity.class);
            hostMapper.decreaseUseNum(hostProp);  //减少host的usernum记录数
        } catch (Exception e) {
            logger.error("账号删除失败");
            logger.error("错误信息");
        }
    }

}